package com.quick.develop.flowable.framework.convert.task;

import cn.hutool.core.util.StrUtil;
import com.quick.develop.framework.utils.StringUtils;
import com.quick.develop.framework.utils.collection.CollectionUtils;
import com.quick.develop.flowable.domain.activity.BpmActivityRespVO;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.runtime.ActivityInstance;
import org.flowable.engine.task.Comment;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.*;

/**
 * BPM 活动 Convert
 *
 * @author
 */
@Mapper
public interface BpmActivityConvert {

    BpmActivityConvert INSTANCE = Mappers.getMapper(BpmActivityConvert.class);
    
    default Map<String, List<BpmActivityRespVO>> convert1(List<BpmActivityRespVO> list,
                                                          Map<String, HistoricProcessInstance> processInstanceMap,
                                                          Map<String, Set<String>> subUsers,
                                                          Map<String, String> names,
                                                          Map<String, Set<String>> candidateUsers,
                                                          Map<String, List<Comment>> comments){
        for (BpmActivityRespVO vo : list) {
            // 任务数据
            Optional.ofNullable(vo.getTaskId()).ifPresent(id -> {
                if (comments.containsKey(id)) {
                    vo.setCommentList(BpmTaskConvert.INSTANCE.convert3(comments.get(id)));
                }
                if (StringUtils.isNotEmpty(vo.getAssignee())) {
                    if (names.containsKey(vo.getAssignee())) {
                        vo.setAssigneeName(names.get(vo.getAssignee()));
                    }
                }else if (candidateUsers.containsKey(id)) {
                    Optional.ofNullable(candidateUsers.get(id)).ifPresent(uu -> vo.setAssigneeName(StrUtil.join(",", uu)));
                }
                
            });
            
            // 节点为调用子流程
            Optional.ofNullable(vo.getCalledProcessInstanceId()).ifPresent(id -> {
                HistoricProcessInstance instance = processInstanceMap.get(id);
                if (Objects.nonNull(instance)) {
                    vo.setCalledProcessInstanceName(instance.getName());
                }
                Set<String> var = subUsers.get(id);
                vo.setAssigneeName(getSubUser(var, names));
            });
            
        }
        
        return CollectionUtils.convertMultiMap(list, BpmActivityRespVO::getActivityId);
    }
    
    default String getSubUser(Set<String> user, Map<String, String> names){
        StringBuilder stringBuilder = new StringBuilder();
        return Optional.ofNullable(user).map(u -> {
            for (String s : u) {
                Optional.ofNullable(names.get(s)).ifPresent(stringBuilder::append);
            }
            
            return stringBuilder.toString();
        }).orElse(null);
    }

    List<BpmActivityRespVO> convertList(List<HistoricActivityInstance> list);

    BpmActivityRespVO convert(HistoricActivityInstance bean);
    
    List<BpmActivityRespVO> convertList1(List<ActivityInstance> list);
}
